// pages/movie/movie.js
var app = getApp();
var util = require("../../util/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inThreaters:{},
    commingSoon:{},
    top250:{},
    searchPanelShow:false,
    containerShow:true,
    searchResult:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var inThreatersUrl = app.globalData.doubanBase + "/v2/movie/in_theaters" + "?start=0&count=3";
    var commingSoonUrl = app.globalData.doubanBase + "/v2/movie/coming_soon" + "?start=0&count=3";
    var top250Url = app.globalData.doubanBase + "/v2/movie/top250" + "?start=0&count=3";
    this.getMovieListData(inThreatersUrl,"inThreaters","正在热映");
    this.getMovieListData(commingSoonUrl, "commingSoon", "即将上映");
    this.getMovieListData(top250Url, "top250", "豆瓣Top250");
  },
  
  // 请求电影的方法
  getMovieListData:function(url,settedKey,categoryTitle){
      var that = this;
      wx.request({
        url: url,
        method:"GET",
        header:{
          "content-type":"json"
        },
        success:function(res){
          // console.log(res.data)
          that.processDoubanData(res.data, settedKey, categoryTitle)
        },
        fail:function(res){
          console.log(res)
        }
      })
  },

  // 整合数据为自定义格式
  processDoubanData: function (movieDouban, settedKey, categoryTitle) {
      var movies = [];
    for (var index in movieDouban.subjects) {
      var subject = movieDouban.subjects[index];
      var title = subject.title;
      if (title.length >= 6) {
        title = title.substring(0,6)+"...";
      }
      var temp= {
        stars: util.convertToStartsArray(subject.rating.stars),
        title:title,
        average: subject.rating.average,
        coverageUrl: subject.images.large,
        movieId:subject.id
      }
      movies.push(temp)
    }
    var readyData = {};
    readyData[settedKey] = {
      categoryTitle: categoryTitle,
      movies: movies
    };
    var tmp = {
      categoryTitle: categoryTitle,
      movies: movies
    };
    console.log(readyData);
    if (settedKey === "inThreaters") {
      this.setData({
        inThreaters: tmp
      });
    } else if (settedKey === "commingSoon") {
      this.setData({
        commingSoon: tmp
      });
    } else if (settedKey === "top250") {
      this.setData({
        top250: tmp
        // readyData
      });
    } else if (settedKey === "searchResult") {
      this.setData({
        searchResult:tmp
      })
    }

  },

  //进入显示更多页面
  onMoreTap:function(event){
    var categoryTitle = event.currentTarget.dataset.category;
    wx.navigateTo({
      url: 'more-movie/more-movie?category=' + categoryTitle,
    })

  },
  
  //搜索框被点击
  onBindFocus:function(event) {
      this.setData({
        searchPanelShow: true,
        containerShow: false
      })
  },
  
  //输入完毕，调取搜索接口
  onBindConfirm:function(event) {
      var keyWord = event.detail.value;
    var searchUrl = app.globalData.doubanBase + "/v2/movie/search?q="+keyWord;
    this.getMovieListData(searchUrl,"searchResult","");
  },

  //点击取消搜索
  onCancelImgTap:function(event) {
    this.setData({
      searchPanelShow: false,
      containerShow: true,
      searchResult:{},
      inputValue:''
    })
  },

  //点击电影进入详情页面
  onMovieTap:function(event) {
    var movieId = event.currentTarget.dataset.movieId;
    wx.navigateTo({
      url: 'movie-detail/movie-detail?id=' + movieId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})